package com.lavegue.webaction;

import java.io.File;
import java.io.IOException;

import javax.servlet.ServletOutputStream;

import org.apache.commons.io.FileUtils;
import org.apache.commons.io.IOUtils;
import org.apache.struts2.ServletActionContext;

import com.lavegue.common.BaseAction;
import com.lavegue.model.Article;
import com.lavegue.service.ArticleService;
import com.lavegue.util.StringUtil;

/**
 * author: luoweibin
 * date:2012-2-20
 * description:
 *
 */
public class ArticleManagerAction extends BaseAction {
	 //易彩通简介 
	public static final int INTRO = 1;
	 //诚聘英才
	public static final int RECRUITMENT = 2;
	//联系我们 
	public static final int CONTACT_OUR = 3;
	//法律声明 
	public static final int LAY_STATMENT = 4;
	//隐私条款 
	public static final int YINSI_TIAOKUAN = 5;
	//安全保障
	public static final int SAFE = 6;
	//客户服务协议
	public static final int KEHU_XIEYI = 7;
	
	private Article article;
	private ArticleService articleService;
	//类型
	private int type;
	//内容
	private String content;
	
	public String update() {
		if (StringUtil.isEmpty(content)) {
			this.addActionError("请输入要更新的内容");
			return show();
		}
		String newContent = content.replace("&nbsp;", "").replace("<p>", "").replace("</p>", "");
		if (StringUtil.isEmpty(newContent)) {
			this.addActionError("请输入要更新的内容");
			return show();
		}
		article = articleService.findById(type);
		article.setContent(content);
		articleService.update(article);
		this.addActionMessage("修改成功");
		return show();
	}

	public String show() {
		article = articleService.findById(type);
		return "articlePage";
	}
	
	public void downExcel() throws IOException {
		File excel = new File(this.getProjectPath() + "download/user_import.xls");
		String fileType = ServletActionContext.getRequest().getParameter("fileType");
		if(fileType.equals("2003")){
			String fname = "user_import.xls";
			ServletActionContext.getRequest().setCharacterEncoding("UTF-8");
			fname = java.net.URLEncoder.encode(fname, "UTF-8");
			ServletActionContext.getResponse().setHeader("Content-Disposition", "attachment; filename="+fname);
			ServletActionContext.getResponse().setContentType("application/msexcel");// 定义输出类型 

		}else{
			String fname = "user_import.xlsx";
			ServletActionContext.getRequest().setCharacterEncoding("UTF-8");
			fname = java.net.URLEncoder.encode(fname, "UTF-8");
			ServletActionContext.getResponse().setHeader("Content-Disposition", "attachment; filename="+fname);
			ServletActionContext.getResponse().setContentType("application/msexcel");// 定义输出类型 

		}
		ServletOutputStream out = ServletActionContext.getResponse().getOutputStream();
		byte[] data = FileUtils.readFileToByteArray(excel);
		IOUtils.write(data, out);
	}

	public Article getArticle() {
		return article;
	}

	public void setArticle(Article article) {
		this.article = article;
	}

	public int getType() {
		return type;
	}


	public void setType(int type) {
		this.type = type;
	}


	public String getContent() {
		return content;
	}

	public void setContent(String content) {
		this.content = content;
	}

	public ArticleService getArticleService() {
		return articleService;
	}

	public void setArticleService(ArticleService articleService) {
		this.articleService = articleService;
	}
}

